#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define init_size 4
#define DATE_TYPE int

typedef struct sqlist
{
    DATE_TYPE *data;
    int len;
    int current_size; // 当前容量
} sqlist, *psqlist;


//声明
//创建空表
int creat_list(psqlist list);
//扩容
int add_size(psqlist list,int size);
//尾插
int tail_insert(psqlist list, DATE_TYPE data);
//删除指定位置数据
int del_list(psqlist list,int pos);
//遍历
int show_list(psqlist list);
//获得指定位置元素
int get_pos_data(psqlist list,int pos);
//获取顺序表长度
int length(psqlist list);



int main(int argc, char const *argv[])
{
    sqlist list;
    creat_list(&list);
    printf("尾插\n");
    tail_insert(&list, 1);
    tail_insert(&list, 2);
    tail_insert(&list, 3);
    tail_insert(&list, 4);
    show_list(&list);

    tail_insert(&list, 5);
    tail_insert(&list, 6);
    show_list(&list);


    printf("删除\n");
    del_list(&list,1);
    show_list(&list);
    del_list(&list,2);
    show_list(&list);
    
    int n;
    n=get_pos_data(&list,1);
    printf("%d\n",list.data[n]);
    n=get_pos_data(&list,4);
    printf("%d\n",list.data[n]);

    n=length(&list);
    printf("length=%d\n",n);

    return 0;
}

//创建空表
int creat_list(psqlist list)
{
    list->data = (DATE_TYPE *)malloc(sizeof(DATE_TYPE) * init_size);
    if (list->data == NULL)
    {
        printf("malloc fialed\n");
        return -1;
    }
    memset(list->data, 0, sizeof(DATE_TYPE) * init_size);
    list->current_size = init_size;
    list->len = 0;
    return 0;
}

//扩容
int add_size(psqlist list,int size)
{
    list->data = (DATE_TYPE *)realloc(list->data, sizeof(DATE_TYPE) * size);
    if (list->data == NULL)
    {
        printf("malloc failed\n");
        return -1;
    }
    list->current_size=size;
    return 0;
}

//尾插
int tail_insert(psqlist list, DATE_TYPE data)
{
    if (list == NULL)
    {
        printf("list is error\n");
        return -1;
    }
    if (list->len >= list->current_size)
    {
       if (0 != add_size(list,2 * list->current_size))
        {
            printf("扩容失败\n");
            return 0;
        }
    }
    list->data[list->len] = data;
    list->len++;
    return 0;
}

//删除指定位置数据
int del_list(psqlist list,int pos)
{
    if(list->len==0||list->data==NULL)
    {
        printf("list is empty\n");
        return -1;
    }
    for (size_t i = pos-1; i < list->len-1; i++)
    {
        list->data[i]=list->data[i+1];
    }
    list->len--;

    memset(&list->data[list->len],0,sizeof(DATE_TYPE));
    if(list->len<list->current_size/4 && list->current_size>init_size)
    {
        add_size(list,list->current_size/2);
    }
    return 0;
}

//获得指定位置元素
int get_pos_data(psqlist list,int pos)
{
    int i=0;
    if(list->len==0||list->data==NULL)
    {
        printf("list is empty\n");
        return -1;
    }
    if(pos<1)
    {
        printf("pos is error\n");
        return -1;
    }
    while(i<pos)
    {
        i++;
    }
    if(i>pos)
    {
        printf("pos is large\n");
        return -1;
    }
    printf("i:%d\n",i-1);
    return i-1;
}

//获取顺序表长度
int length(psqlist list)
{
  if(list->data==NULL)
  {
    return -1;
  }
  return list->len;
}


//遍历
int show_list(psqlist list)
{
    if (list->len == 0 || list->data == NULL)
    {
        return -1;
    }
    for (size_t i = 0; i < list->len; i++)
    {
        printf("%d\t", list->data[i]);
    }
    printf("\n");
    return 0;
}
